package com.ruoyi.web.controller.web3j;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.web3jutil.Web3jUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.methods.response.EthBlockNumber;
import org.web3j.protocol.core.methods.response.EthGasPrice;
import org.web3j.protocol.core.methods.response.Web3ClientVersion;
import org.web3j.utils.Convert;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;

/**
 * 区块链网络信息控制器
 * 
 * @author ruoyi
 */
@Api(tags = "区块链网络信息管理")
@RestController
@RequestMapping("/web3j/info")
public class Web3jInfoController {

    /**
     * 获取区块链客户端版本
     * 
     * @return 客户端版本信息
     */
    @ApiOperation(value = "获取区块链客户端版本", notes = "返回当前连接的区块链客户端版本信息")
    @GetMapping("/client-version")
    public AjaxResult getClientVersion() {
        try {
            Web3j web3j = Web3jUtil.getWeb3j();
            Web3ClientVersion clientVersion = web3j.web3ClientVersion().send();
            return AjaxResult.success("获取客户端版本成功", clientVersion.getWeb3ClientVersion());
        } catch (IOException e) {
            return AjaxResult.error("获取客户端版本失败: " + e.getMessage());
        }
    }

    /**
     * 获取当前区块高度
     * 
     * @return 当前区块高度
     */
    @ApiOperation(value = "获取当前区块高度", notes = "返回区块链网络的当前区块高度")
    @GetMapping("/block-number")
    public AjaxResult getBlockNumber() {
        try {
            Web3j web3j = Web3jUtil.getWeb3j();
            EthBlockNumber blockNumber = web3j.ethBlockNumber().send();
            return AjaxResult.success("获取区块高度成功", blockNumber.getBlockNumber());
        } catch (IOException e) {
            return AjaxResult.error("获取区块高度失败: " + e.getMessage());
        }
    }

    /**
     * 获取当前Gas价格
     * 
     * @return 当前Gas价格
     */
    @ApiOperation(value = "获取当前Gas价格", notes = "返回区块链网络的当前Gas价格，包括Wei和Gwei单位")
    @GetMapping("/gas-price")
    public AjaxResult getGasPrice() {
        try {
            Web3j web3j = Web3jUtil.getWeb3j();
            EthGasPrice gasPrice = web3j.ethGasPrice().send();
            BigInteger weiValue = gasPrice.getGasPrice();
            BigDecimal gweiValue = Convert.fromWei(new BigDecimal(weiValue), Convert.Unit.GWEI);
            
            return AjaxResult.success("获取Gas价格成功", new GasPriceInfo(
                    weiValue.toString(),
                    gweiValue.toString()
            ));
        } catch (IOException e) {
            return AjaxResult.error("获取Gas价格失败: " + e.getMessage());
        }
    }

    /**
     * Gas价格信息内部类
     */
    static class GasPriceInfo {
        private String weiValue;
        private String gweiValue;

        public GasPriceInfo(String weiValue, String gweiValue) {
            this.weiValue = weiValue;
            this.gweiValue = gweiValue;
        }

        public String getWeiValue() {
            return weiValue;
        }

        public void setWeiValue(String weiValue) {
            this.weiValue = weiValue;
        }

        public String getGweiValue() {
            return gweiValue;
        }

        public void setGweiValue(String gweiValue) {
            this.gweiValue = gweiValue;
        }
    }
}